package com.lzxuni.modules.weather.controller;


import com.alibaba.fastjson.JSON;
import com.lzxuni.common.utils.FileUtil;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.date.DateUtil;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.weather.entity.WeatherInfo;
import com.lzxuni.modules.weather.service.WeatherInfoForecastHourService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.Date;

/**
 * <p>
 * 天气信息_预测小时天气管理 前端控制器
 * </p>
 *
 * @author LaoNa
 * @since 2020-11-27
 */
@RestController
@RequestMapping("/Weather/InfoForecastHour")
public class WeatherInfoForecastHourController extends BaseController {

    @Autowired
    private WeatherInfoForecastHourService weatherInfoForecastHourService;
    private String templatesPath="/Weather/InfoForecastHour";

    //主页面
    @RequestMapping("/Index")
    public ModelAndView Index() {
        ModelAndView mv = new ModelAndView(templatesPath+"/index");
        return mv;
    }

    //查询数据
    @RequestMapping("/GetList")
    public Object GetList(String pagination,String queryJson){
        PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
        WeatherInfo weatherInfo = JSON.parseObject(queryJson, WeatherInfo.class);
        PageData pageData = getPageData(weatherInfoForecastHourService.queryPage(pageParameter, weatherInfo));
        return R.ok().put("data", pageData);
    }




    @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response, String queryJson, HttpServletRequest request) throws IOException {
        WeatherInfo weatherInfo = JSON.parseObject(queryJson, WeatherInfo.class);

        BufferedOutputStream fos = new BufferedOutputStream(response.getOutputStream());
        String filename = "预测12小时天气列表" + DateUtil.DateToString(new Date(), "yyyyMMddHHmm") + ".xls";
        String agent = request.getHeader("User-Agent");
        filename = FileUtil.encodeDownloadFilename(filename, agent);
        String contentType = request.getSession().getServletContext().getMimeType(filename);
        response.setContentType(contentType);
        response.setHeader("content-disposition", "attchment;filename=" + filename);
        Workbook wb = null;
        try {
            wb = weatherInfoForecastHourService.exportExcel(weatherInfo);
            wb.write(fos);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            fos.close();
        }
    }


}

